package com.buaa.airticketbookingsystem.Controller;

import com.buaa.airticketbookingsystem.Entity.PurchaseRecord;
import com.buaa.airticketbookingsystem.Service.PurchaseRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
@CrossOrigin
@RestController
public class GiveUpController {

    @Autowired
    private PurchaseRecordService purchaseRecordService;

    @PostMapping("/giveUp")
    public Map<String, Object> giveUpPurchase(@RequestBody Map<String, String> remap){
        String o_id = remap.get("no");
        Map<String, Object> resMap = new HashMap<>();
        PurchaseRecord pr = purchaseRecordService.getPurchaseRecordByNo(o_id);
        if(pr == null){
            resMap.put("res", false);
            resMap.put("message", "查询购买记录为空");
            return resMap;
        } else{
            try{
                purchaseRecordService.deletePurchaseRecordByIdAndEmail(o_id, pr.getT_id());
                resMap.put("res", true);
                resMap.put("message", "取消订单成功");
                return resMap;
            } catch (Exception e){
                e.printStackTrace();
                resMap.put("res", false);
                resMap.put("message", "抛出异常");
                return resMap;
            }
        }
    }
}
